package com.braveman.im.utils;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

import com.ht.baselibs.application.HTApplication;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechSynthesizer;
import com.braveman.im.R;

/**
 * **************************************************************
 * [好刷刷]
 * **************************************************************
 * Authors: 黄涛 on 17/1/5 11:25
 * QQ：876046815
 * Email:876046815@qq.com
 */
public class VoiceUtils {
    private static SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(HTApplication.getContext(), null);

    static {
        mTts.setParameter(SpeechConstant.PITCH, "50");
        mTts.setParameter(SpeechConstant.VOLUME, "100");
    }

    public static void playDetails(String content){
        AudioManager audio = (AudioManager) HTApplication.getContext().getSystemService(Context.AUDIO_SERVICE);
        int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
        audio.setStreamVolume(AudioManager.STREAM_MUSIC, audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC)%80, 0);
        mTts.startSpeaking(content, null);
        audio.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
    }

    public static void stopDetails(){
        if (mTts.isSpeaking()){
            mTts.stopSpeaking();
        }
    }


    private static SoundPool soundPool;
    private static int mSoundId;

    static{
        if(soundPool == null){
            soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC,100);
        }
        mSoundId = soundPool.load(HTApplication.getContext(), R.raw.success, 1);
    }

    public static void  play(){
        soundPool.play(mSoundId, 1, 1, 0, 0, 1);
    }

    public static void stop(){
        soundPool.stop(mSoundId);
    }
}
